package cc.rengu.igas.cops.common.dao.impl;

import cc.rengu.igas.cops.common.dao.CopsSignAcctInfoMapper;
import cc.rengu.igas.cops.common.entity.CopsOpmsUserInfoAndSignAcctInfo;
import cc.rengu.igas.cops.common.enums.CopsRespCodeEnum;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.SignAcctInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 签约信息表操作实现
 * @Author panky
 * @date 2020/5/22
 **/
public class CopsSignAcctInfoMapperImpl implements CopsSignAcctInfoMapper {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);

    @Override
    public SignAcctInfo selectSignInfoBySignTokenTypeSignInstSignTypeAcctNo(String instId, String signTokenType, String signInst, String signType, String acctNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsGetSignInfoBySignTokenTypeSignInstSignTypeAcctNo");

        SignAcctInfo signAcctInfo = new SignAcctInfo();
        signAcctInfo.setInstId(instId);
        signAcctInfo.setSignAcctNo(acctNo);
        signAcctInfo.setSignTokenType(signTokenType);
        signAcctInfo.setSignInst(signInst);
        signAcctInfo.setSignType(signType);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, signAcctInfo, signAcctInfo);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            rglog.error("签约信息表中不存在该数据,SIGN_TOKEN_TYPE:<{}> ,SIGN_INST:<{}>,CERT_NO:<{}> ,SIGN_ACCT_NO:<{}>", signTokenType, signInst, signAcctInfo.getCertNo(), signAcctInfo.getSignAcctNo());
            return null;
        } else if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("查询签约信息表失败,SIGN_TOKEN_TYPE:<{}> ,SIGN_INST:<{}>,CERT_NO:<{}> ,SIGN_ACCT_NO:<{}>", signTokenType, signInst, signAcctInfo.getCertNo(), signAcctInfo.getSignAcctNo());
            throw new BizException(CopsRespCodeEnum.ORDER_NOT_EXIT.getRespCode(), CopsRespCodeEnum.ORDER_NOT_EXIT.getRespDesc());
        }
        return signAcctInfo;
    }

    @Override
    public int insertSignAcctInfo(SignAcctInfo info) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsInsertSignAcctInfo");

        int recode = dbsUtil.dbsExecuteSql(sqlStr, info, info);
        if (recode != Database.DBS_SUCCESS) {
            rglog.info("插入数据库失败");
        }
        return recode;
    }

    @Override
    public int insertSignAcctInfoWithTrancation(SignAcctInfo info, DbsUtil dbsUtil) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        /*String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);*/
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsInsertSignAcctInfo");

        int recode = dbsUtil.dbsExecuteTransactionSql(sqlStr, info, info);
        if (recode != Database.DBS_SUCCESS) {
            rglog.info("插入数据库失败");
        }
        return recode;
    }

    @Override
    public List<SignAcctInfo> selectSignAcctInfoBySignInstAcctNo(String instId, String signInst, String acctNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsGetSignAcctInfoBySignInstAcctNo");

        SignAcctInfo signAcctInfo = new SignAcctInfo();
        signAcctInfo.setInstId(instId);
        signAcctInfo.setSignInst(signInst);
        signAcctInfo.setSignAcctNo(acctNo);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, signAcctInfo, SignAcctInfo.class);
        return objects.stream().map(item -> (SignAcctInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<SignAcctInfo> selectSignAcctInfoBySignInstSignTypeAcctNo(String instId, String signInst, String signType, String acctNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsGetSignAcctInfoBySignInstSignTypeAcctNo");

        SignAcctInfo signAcctInfo = new SignAcctInfo();
        signAcctInfo.setInstId(instId);
        signAcctInfo.setSignInst(signInst);
        signAcctInfo.setSignType(signType);
        signAcctInfo.setSignAcctNo(acctNo);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, signAcctInfo, SignAcctInfo.class);
        return objects.stream().map(item -> (SignAcctInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<SignAcctInfo> selectSignAcctInfoBySingInstCertTypeCertNo(String instId, String signInst, String certType, String certNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsGetSignAcctInfoBySingInstCertTypeCertNo");

        SignAcctInfo signAcctInfo = new SignAcctInfo();
        signAcctInfo.setInstId(instId);
        signAcctInfo.setSignInst(signInst);
        signAcctInfo.setCertType(certType);
        signAcctInfo.setCertNo(certNo);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, signAcctInfo, SignAcctInfo.class);
        if (objects == null || objects.isEmpty()) {
            return null;
        }
        return objects.stream().map(item -> (SignAcctInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<SignAcctInfo> selectSignAcctInfoBySignInstReservePhoneNo(String instId, String signInst, String reservePhoneNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsGetSignAcctInfoBySignInstReservePhoneNo");

        SignAcctInfo signAcctInfo = new SignAcctInfo();
        signAcctInfo.setInstId(instId);
        signAcctInfo.setSignInst(signInst);
        signAcctInfo.setReserveMobileNo(reservePhoneNo);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, signAcctInfo, SignAcctInfo.class);
        return objects.stream().map(item -> (SignAcctInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<SignAcctInfo> selectSignAcctInfoBySignAcctNo(String instId, String signAcctNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsGetSignAcctInfoBySignAcctNo");

        SignAcctInfo signAcctInfo = new SignAcctInfo();
        signAcctInfo.setInstId(instId);
        signAcctInfo.setSignAcctNo(signAcctNo);
        signAcctInfo.setSignStatus(AppParamConstant.YES);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, signAcctInfo, SignAcctInfo.class);
        return objects.stream().map(item -> (SignAcctInfo) item).collect(Collectors.toList());
    }

    @Override
    public int updateSignAcctInfoByPriKeyNoTransaction(SignAcctInfo signAcctInfo, DbsUtil dbs) throws Exception {
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsupdateSignAcctInfoByPriKey");
        return dbs.dbsExecuteTransactionSql(sqlStr, signAcctInfo, null);
    }

    @Override
    public int updateSignAcctInfoByPriKey(SignAcctInfo signAcctInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsupdateSignAcctInfoByPriKey");
        return dbsUtil.dbsExecuteSql(sqlStr, signAcctInfo, null);
    }

    @Override
    public List<SignAcctInfo> selectSignAcctInfoByUserId(String instId, String signInst, String userId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsSelectSignAcctInfoByUserId");

        SignAcctInfo signAcctInfo = new SignAcctInfo();
        signAcctInfo.setInstId(instId);
        signAcctInfo.setSignInst(signInst);
        signAcctInfo.setUserId(userId);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, signAcctInfo, SignAcctInfo.class);
        return objects.stream().map(item -> (SignAcctInfo) item).collect(Collectors.toList());
    }

    @Override
    public int updateSignAcctInfoBySignTokenTypeSignInstSignTypeAcctNo(SignAcctInfo signAcctInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsUpdateSignAcctInfoBySignTokenTypeSignInstSignTypeAcctNo");
        return dbsUtil.dbsExecuteSql(sqlStr, signAcctInfo, null);
    }

    @Override
    public PageResult selectAcctSignInfoByNotNullParams(String instId, String certType, String certNo, String cardNo, String signStatus, String startDate, String endDate, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectAcctSignInfoByNotNullParams");
        CopsOpmsUserInfoAndSignAcctInfo copsOpmsUserInfoAndSignAcctInfo = new CopsOpmsUserInfoAndSignAcctInfo();
        copsOpmsUserInfoAndSignAcctInfo.setInstId(instId);
        copsOpmsUserInfoAndSignAcctInfo.setCertNo(certNo);
        copsOpmsUserInfoAndSignAcctInfo.setCertType(certType);
        copsOpmsUserInfoAndSignAcctInfo.setSignAcctNo(cardNo);
        copsOpmsUserInfoAndSignAcctInfo.setCreateTime(startDate);
        copsOpmsUserInfoAndSignAcctInfo.setLastUpdateTime(endDate);
        copsOpmsUserInfoAndSignAcctInfo.setSignStatus(signStatus);

        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, copsOpmsUserInfoAndSignAcctInfo, CopsOpmsUserInfoAndSignAcctInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("根据证件信息查询签约账户信息失败,instId:<{}>,certNo:<{}>，certType=<{}>,card_no = <{}>,userStatus = <{}>,startDate = <{}>,endDate =<{}>", instId, certNo, certType, cardNo, signStatus, startDate, endDate);
            return null;
        }
        return pageResult;
    }

    @Override
    public List<SignAcctInfo> selectSignAcctInfoByInstIdCertTypeCertNo(String instId, String certType, String certNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsSelectSignAcctInfoByInstIdCertTypeCertNo");

        SignAcctInfo signAcctInfo = new SignAcctInfo();
        signAcctInfo.setInstId(instId);
        signAcctInfo.setCertType(certType);
        signAcctInfo.setCertNo(certNo);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, signAcctInfo, SignAcctInfo.class);
        return objects.stream().map(item -> (SignAcctInfo) item).collect(Collectors.toList());
    }

    @Override
    public int updateSignAcctInfoByPriKeyWithTransaction(SignAcctInfo signAcctInfo, DbsUtil dbsUtil) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CopsupdateSignAcctInfoByPriKey");
        return dbsUtil.dbsExecuteTransactionSql(sqlStr, signAcctInfo, null);
    }

    @Override

    public int updateSignStateBySignAcct(SignAcctInfo signAcctInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateSignStateBySignAcct");

        int recode = dbsUtil.dbsExecuteSql(sqlStr, signAcctInfo, signAcctInfo);
        if (recode != Database.DBS_SUCCESS) {
            rglog.info("更新签状态失败");
        }
        return recode;
    }
}
